package org.karelun.gef.property;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import org.karelun.gef.helper.ConstantMessages;
import org.karelun.gef.helper.IDConstant;

public class ConstraintPropertySource implements IPropertySource {

	private static final String ID_XPOS = "xPos"; //$NON-NLS-1$
	private static final String ID_YPOS = "yPos"; //$NON-NLS-1$
	private static final String ID_WPOS = "wPos"; //$NON-NLS-1$
	private static final String ID_HPOS = "hPos"; //$NON-NLS-1$

	private Rectangle constraint = null;

	public ConstraintPropertySource(Rectangle constraint) {
		this.constraint = constraint;
	}

	public Object getEditableValue() {
		return constraint.getCopy();
	}

	public IPropertyDescriptor[] getPropertyDescriptors() {
		IPropertyDescriptor[] descriptors = new IPropertyDescriptor[] {
				new TextPropertyDescriptor(ID_XPOS, ConstantMessages.property_X),
				new TextPropertyDescriptor(ID_YPOS, ConstantMessages.property_Y),
				new TextPropertyDescriptor(ID_WPOS,
						ConstantMessages.property_Width),
				new TextPropertyDescriptor(ID_HPOS,
						ConstantMessages.property_Heigh) };
		for (IPropertyDescriptor p : descriptors) {
			((PropertyDescriptor) p)
					.setLabelProvider(new PropertyLabelProvider(IDConstant.Constraint_prop));
		}
		return descriptors;
	}

	public Object getPropertyValue(Object propName) {
		if (ID_XPOS.equals(propName)) {
			return String.valueOf(constraint.x);
		}
		if (ID_YPOS.equals(propName)) {
			return String.valueOf(constraint.y);
		}
		if (ID_WPOS.equals(propName)) {
			return String.valueOf(constraint.width);
		}
		if (ID_HPOS.equals(propName)) {
			return String.valueOf(constraint.height);
		}
		return null;
	}

	public boolean isPropertySet(Object id) {
		return ID_XPOS.equals(id) || ID_YPOS.equals(id) || ID_WPOS.equals(id)
				|| ID_HPOS.equals(id);
	}

	public void resetPropertyValue(Object id) {
	}

	public void setPropertyValue(Object id, Object value) {
		int x = constraint.x;
		int y = constraint.y;
		int width = constraint.width;
		int height = constraint.height;
		try {
			String str = (String) value;
			int val = Integer.parseInt(str);
			if (id.equals(ID_XPOS)) {
				x = val;
			} else if (id.equals(ID_YPOS)) {
				y = val;
			} else if (id.equals(ID_WPOS)) {
				width = val;
			} else if (id.equals(ID_HPOS)) {
				height = val;
			}
		} catch (Exception e) {
			// MessageDialog.openError(null, "Input Error",
			// "Please input the number");
			// e.printStackTrace();
		} finally {
			constraint.x = x;
			constraint.y = y;
			constraint.width = width;
			constraint.height = height;
		}
	}

	public String toString() {
		StringBuilder str = new StringBuilder();
		str.append("Constraint{x="); //$NON-NLS-1$
		str.append(constraint.x);
		str.append(" , y="); //$NON-NLS-1$
		str.append(constraint.y);
		str.append(" , width="); //$NON-NLS-1$
		str.append(constraint.width);
		str.append(" ,height"); //$NON-NLS-1$
		str.append(constraint.height);
		str.append("}"); //$NON-NLS-1$
		return str.toString();
	}
}
